package com.denlaku.longan.service.impl;

import cn.idev.excel.EasyExcel;
import cn.idev.excel.ExcelWriter;
import cn.idev.excel.write.metadata.WriteSheet;
import com.denlaku.longan.anno.RequiredTx;
import com.denlaku.longan.qo.Query;
import com.denlaku.longan.repository.DataModelRepository;
import com.denlaku.longan.service.DataModelItemService;
import com.denlaku.longan.service.DataModelService;
import com.denlaku.longan.util.SnowflakeUtils;
import com.denlaku.longan.vo.DataModel;
import com.denlaku.longan.vo.DataModelItem;
import lombok.SneakyThrows;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author tianx
 */
@Service
public class DataModelServiceImpl implements DataModelService {
    @Autowired
    private DataModelRepository dataModelRepository;

    @Autowired
    private DataModelItemService dataModelItemService;

    @Override
    @RequiredTx
    public void add(DataModel dataModel) {
        dataModel.setId(SnowflakeUtils.nextId());
        dataModel.setStatus("1");
        dataModelRepository.add(dataModel);
        batchAddItem(dataModel);
    }

    @Override
    @RequiredTx
    public void update(DataModel dataModel) {
        dataModelRepository.update(dataModel);
        dataModelItemService.delete(dataModel.getTenantId(), dataModel.getId());
        batchAddItem(dataModel);
    }

    private void batchAddItem(DataModel dataModel) {
        ListUtils.emptyIfNull(dataModel.getItemList()).forEach(item -> {
            item.setDataModelId(dataModel.getId());
            item.setId(SnowflakeUtils.nextId());
        });
        dataModelItemService.batchAdd(dataModel.getItemList());
    }

    @Override
    public void delete(Query query) {
        dataModelRepository.delete(query);
    }

    @Override
    public DataModel get(Query query) {
        DataModel dataModel = dataModelRepository.get(query);
        if (dataModel != null) {
            List<DataModelItem> itemList = dataModelItemService.list(query.getTenantId(), query.getId());
            dataModel.setItemList(itemList);
        }
        return dataModel;
    }

    @Override
    public List<DataModel> list(DataModel dataModel) {
        return dataModelRepository.list(dataModel);
    }

    @Override
    @SneakyThrows
    public File template(Query query) {
        DataModel dataModel = this.get(query);
        if (dataModel == null || CollectionUtils.isEmpty(dataModel.getItemList())) {
            return null;
        }
        File file = File.createTempFile(String.valueOf(SnowflakeUtils.nextId()), null);
        try (ExcelWriter excelWriter = EasyExcel.write(file).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet()
                    .head(initHead(dataModel.getItemList()))
                    .sheetName("template").build();
            excelWriter.write(Collections.emptyList(), writeSheet);
            excelWriter.finish();
        }
        return file;
    }

    private List<List<String>> initHead(List<DataModelItem> itemList) {
        List<List<String>> head = new ArrayList<>();
        for (DataModelItem dataModelItem : itemList) {
            head.add(Collections.singletonList(dataModelItem.getName()));
        }
        return head;
    }

    @Override
    public void dataImport(Query query) {

    }

    @Override
    public void dataExport(Query query) {

    }

}
